/**
 * 
 */
package org.openwork.portlet.skillmanagement.permission;

import org.openwork.portlet.skillmanagement.model.SkillCategory;
import org.openwork.portlet.skillmanagement.service.SkillCategoryLocalServiceUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.security.permission.PermissionChecker;

/**
 * @author samuelgmartinez
 *
 */
public class SkillCategoryPermissionUtil {

	public static void canCreate(PermissionChecker permissionChecker, long groupId) throws PortalException {
		if(!permissionChecker.hasPermission(groupId, SkillCategory.class.getName(), groupId, SkillUserActions.UPDATE)) {
			throw new PrincipalException();
		}
	}
	
	public static void check(PermissionChecker permissionChecker, long pk, String actionId) throws PortalException, SystemException {
		if(!contains(permissionChecker, pk, actionId)) {
			throw new PrincipalException();
		}
	}
	
	public static boolean contains(PermissionChecker permissionChecker, SkillCategory category, String actionId) {
		if(permissionChecker.hasOwnerPermission(category.getGroup().getCompanyId(), 
				SkillCategory.class.getName(), category.getId(), category.getUserId(), actionId)) {
			return true;
		}
		
		return permissionChecker.hasPermission(category.getGroupId(), SkillCategory.class.getName(), category.getId(), actionId);
	}
	
	public static boolean contains(PermissionChecker permissionChecker, long pk, String actionId) throws PortalException, SystemException {
		SkillCategory category = SkillCategoryLocalServiceUtil.getSkillCategory(pk);
		
		return contains(permissionChecker, category, actionId);
	}
	
}
